<template>
  <list-card>
    <el-form ref="queryForm" slot="search" :inline="true" :model="queryParams" size="small">
      <el-form-item label="岗位编码" prop="code">
        <el-input v-model="queryParams.code" clearable placeholder="请输入岗位编码" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="岗位名称" prop="name">
        <el-input v-model="queryParams.name" clearable placeholder="请输入岗位名称" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="岗位状态">
          <el-option v-for="item in localDict.post_status" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div slot="operate">
      <el-button v-has-permi="['system:post:add']" icon="el-icon-plus" plain size="mini" type="primary"
                 @click="handleAdd">
        新增
      </el-button>
      <el-button v-has-permi="['system:post:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
                 type="danger" @click="handleDelete">删除
      </el-button>
      <el-button v-has-permi="['system:post:export']" icon="el-icon-download" plain size="mini" type="warning"
                 @click="handleExport">导出
      </el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>
    <el-table slot="content" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="岗位编码" prop="code"/>
      <el-table-column align="center" label="岗位名称" prop="name"/>
      <el-table-column align="center" label="岗位排序" prop="sort"/>
      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="localDict.post_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime" width="180"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-has-permi="['system:post:edit']" icon="el-icon-edit" size="mini" type="text"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-has-permi="['system:post:remove']" icon="el-icon-delete" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="pageParam.total > 0" slot="page" :limit.sync="pageParam.pageSize" :page.sync="pageParam.pageNum"
                :total="pageParam.total" @pagination="getList"/>
    <post-model ref="model" @ok="getList"/>
  </list-card>
</template>

<script>
import {list} from "@/api/base";
import {getStaticData} from "@/api/common";
import ListCard from "@/components/Card/ListCard.vue";
import PostModel from "@/views/system/post/components/ModelCard.vue";
import {remove} from "../../../api/base";

export default {
  name: "Post",
  components: {PostModel, ListCard},
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      baseUrl: '/system/post/',
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 岗位表格数据
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        code: undefined,
        name: undefined,
        status: undefined
      },
      localDict: {
        post_status: []
      },
    };
  },
  created() {
    this.getLocalDict();
    this.resetQuery()
  },
  methods: {
    getLocalDict() {
      getStaticData('system-status').then(res => {
        this.localDict.post_status = res.data
      })
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(res => {
        this.dataList = res.rows;
        this.pageParam.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.model.add()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.model.edit(row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || this.ids;
      this.$modal.confirm('是否确认删除选择的岗位？').then(() => {
        remove(this.baseUrl, postIds, postIds.length > 1).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
